/**
 * 下拉框以及枚举
 */
import commonPkg from '@/utils/commonPkg';

const {
  api: {
    userApi: { getUserList },
  },
} = commonPkg;

let initState = () => ({
  // 用户列表
  userLIstMap: {},
});

export default {
  namespace: 'select',
  state: { ...initState() },
  effects: {
    // 数据源列表
    *getUserList({ payload }, { call, put }) {
      const rs = yield call(getUserList, { params: payload });
      if (rs.success) {
        var userLIstMap = {};
        rs.data.dataList.forEach((item, i) => {
          userLIstMap[item.id] = {
            ...item,
            value: item.id,
            title: item.name,
          };
        });
        yield put({
          type: 'setState',
          payload: { userLIstMap: userLIstMap },
        });
      }
      return Promise.resolve(rs);
    },
  },

  reducers: {
    setState(state, action) {
      return { ...state, ...action.payload };
    },
    init(state, action) {
      return { ...initState(), ...action.payload };
    },
  },
};
